﻿Imports SEREADERLib
Imports System.Runtime.InteropServices
Public Class Form1
    Public strLogFileName As String = String.Empty
    Private Structure AsmData
        Dim FileName As String
        Dim Matrix As Object
        Dim Range As Object
        Dim ParentIndex As Integer
        Dim OccProp As Integer
        Dim OccExtProp As Integer
        Dim OccName As String
        Dim Quantity As Integer
        Dim NonGraphQty As Double
        Dim NonGraphQtyUnit As String
        Dim HideInDwg As Boolean
        Dim HideInSubAsm As Boolean
    End Structure
    Public strInputFilename As String = ""
    Public filenameAsm As String



    Private Sub ReadAsm(ByVal FirstCall As Boolean, ByVal ParentIndex As Integer, ByVal FileName As String, ByRef AssemblyData() As AsmData)
        ' SEReader
        Dim objSEReader As SEREADERLib.SEFileReader
        Dim objAttachments As SEREADERLib.Attachments
        Dim objAttachment As SEREADERLib.Attachment
        Dim obj As Object
        Dim Matrix As Object
        Dim Range As Object
        Dim Names() As String
        'SEInstalldata
        Dim Buildno, MajorNo, MinorNo As Integer
        Dim Version, installpath As String
        Dim mask As Boolean

        objSEReader = CreateObject("SEreader.SEFileReader")

        If Not objSEReader Is Nothing Then
            On Error Resume Next

            objSEReader.Open(FileName)

            If Err.Number > 0 Then
                If Not Err.Number = 5 Then
                    MsgBox(Err.Number)
                End If
                Err.Clear()
            Else
                On Error GoTo 0

                objAttachments = objSEReader.Attachments
                If Not objAttachments Is Nothing Then
                    For Each objAttachment In objAttachments

                        If Not FirstCall Then
                            ReDim Preserve AssemblyData(UBound(AssemblyData) + 1)
                        End If

                        AssemblyData(UBound(AssemblyData)).HideInDwg = False
                        AssemblyData(UBound(AssemblyData)).HideInSubAsm = False

                        FirstCall = False
                        AssemblyData(UBound(AssemblyData)).Matrix = objAttachment.Matrix
                        AssemblyData(UBound(AssemblyData)).Range = objAttachment.Range
                        AssemblyData(UBound(AssemblyData)).FileName = objAttachment.Filename
                        AssemblyData(UBound(AssemblyData)).ParentIndex = ParentIndex
                        AssemblyData(UBound(AssemblyData)).OccProp = objAttachment.OccurrenceProps
                        AssemblyData(UBound(AssemblyData)).OccExtProp = objAttachment.OccurrenceExtendedProps
                        AssemblyData(UBound(AssemblyData)).Quantity = objAttachment.Quantity
                        AssemblyData(UBound(AssemblyData)).OccName = objAttachment.OccurrenceName
                        AssemblyData(UBound(AssemblyData)).NonGraphQty = objAttachment.NonGraphicQuantity


                        If objAttachment.OccurrenceProps And OccurrenceProperties.JAsmHideInDrawing Then
                            AssemblyData(UBound(AssemblyData)).HideInDwg = True
                        End If


                        If AssemblyData(UBound(AssemblyData)).OccProp And SEREADERLib.OccurrenceProperties.JAsmHideInSubAssembly Then
                            AssemblyData(UBound(AssemblyData)).HideInSubAsm = True
                        End If

                        If objAttachment.Filename <> filenameAsm Then ReadAsm(False, UBound(AssemblyData), objAttachment.Filename, AssemblyData)

                    Next
                End If
            End If
        End If

    End Sub

    Private Sub ProcessParts(ByVal AssemblyData() As AsmData)

        Dim i As Integer
        For i = LBound(AssemblyData) To UBound(AssemblyData)
            If Not AssemblyData(i).ParentIndex = -1 Then
                ListBox1.Items.Add("Filename is " & AssemblyData(i).FileName + "  Occ Name = " & AssemblyData(i).OccName & ":   Parent file is " & AssemblyData(AssemblyData(i).ParentIndex).FileName)
                System.IO.File.AppendAllText(strLogFileName, AssemblyData(i).FileName + Environment.NewLine)

            Else
                ListBox1.Items.Add("Filename is " & AssemblyData(i).FileName + "  Occ Name = " & AssemblyData(i).OccName)
                System.IO.File.AppendAllText(strLogFileName, AssemblyData(i).FileName + Environment.NewLine)
            End If
           
        Next i
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With OpenFileDialog1
            .Filter = "Input Files (*.asm,*.csv) | *.asm;*.csv"
            .ShowDialog()
        End With
        Me.TxtFileName.Text = OpenFileDialog1.FileName
        strInputFilename = Me.TxtFileName.Text
        filenameAsm = OpenFileDialog1.FileName
        OpenFileDialog1.Dispose()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim objSEReader As SEFileReader
        Dim ObjSEReaderType As Type
        Dim Names() As String
        Dim Parts() As AsmData
        ReDim Parts(0)


        strLogFileName = "ListOfLinkedComponents.txt"
        strLogFileName = System.IO.Path.GetTempPath + strLogFileName


        Try
            If System.IO.File.Exists(strLogFileName) = True Then
                System.IO.File.Delete(strLogFileName)
            End If
        Catch ex As Exception

        End Try

        ObjSEReaderType = Type.GetTypeFromProgID("SEreader.SEFileReader")
        objSEReader = Activator.CreateInstance(ObjSEReaderType)


        If Not objSEReader Is Nothing Then
            objSEReader.Open(strInputFilename)

            If objSEReader.FamilyOfAssemblies Then
                objSEReader.GetFamilyMemberNames(Names)
                Dim i As Integer
                For i = LBound(Names) To UBound(Names)
                    Debug.Print(Names(i))
                    Me.ListBox1.Items.Add(Names(i))
                    ReadAsm(True, -1, strInputFilename & "!" & Names(i), Parts)
                    ProcessParts(Parts)
                    ReDim Parts(0)
                Next i
            Else
                ReadAsm(True, -1, strInputFilename, Parts)
                ProcessParts(Parts)
            End If
        End If

        Me.Label1.Text = "See file " + strLogFileName + " for the list of linked components"
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        End

    End Sub
End Class
